﻿using System;
using System.Collections.Generic;
using System.Linq;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.CommonControls;
using DevelopTool.Forms.CommonControls.MarkdownEditor;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Forms.Properties;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using Sunny.UI;

namespace DevelopTool.Forms.DocumentForms
{
    /// <summary>
    /// 开发文档备忘录
    /// 部分引用：https://github.com/jaywcjlove/reference
    /// </summary>
    public partial class DevelopDocumentForm : BaseUIPage
    {
        private List<ManageMenu> manageMenus;

        public DevelopDocumentForm()
        {
            InitializeComponent();
        }

        private void DevelopDocumentForm_Load(object sender,EventArgs e)
        {
            LoadingHelper.ShowLoading("正在准备文档",() =>
            {
                this.manageMenus = ZipHelper.ExtractToManageMenu(Resources.DevelopDocument);
            });

            #region 删除最外面的目录
            this.manageMenus.RemoveAll(it => it.Id == 1);
            this.manageMenus = this.manageMenus.Select(it =>
            {
                ManageMenu manageMenu = it;
                if(manageMenu.NodePid == 1) manageMenu.NodePid = 0;

                return manageMenu;
            }).ToList();
            #endregion 删除最外面的目录

            this.manageControl.SetMenuData(this.manageMenus,"文档列表");
        }


        private void manageControl_MenuDoubleClick(object sender,MenuEventArgs e)
        {
            //if(e.ManageMenu.NodeType == MenuNodeType.Node)
            //{
            //    //打开Markdown编辑窗口
            //    MarkdownEditorControl markdownEditorControl = new MarkdownEditorControl();
            //    markdownEditorControl.ShowEditorControl = false;
            //    markdownEditorControl.FileContent = e.ManageMenu.Remark;
            //    markdownEditorControl.Text = e.ManageMenu.NodeName;
            //    this.manageControl.ShowPageForm(markdownEditorControl,e.ManageMenu.NodeName,true,e.ManageMenu.NodeName + e.ManageMenu.Id);                
            //}


            if (e.ManageMenu.NodeType == MenuNodeType.Node)
            {
                //打开Markdown编辑窗口
                MarkdownEditorControl2 markdownEditorControl = new MarkdownEditorControl2();
                markdownEditorControl.EditorMode = "wysiwyg";
                markdownEditorControl.IsCanEdited = false;
                markdownEditorControl.FileContent = e.ManageMenu.Remark;
                markdownEditorControl.Text = e.ManageMenu.NodeName;
                markdownEditorControl.Outline = true;
                this.manageControl.ShowPageForm(markdownEditorControl, e.ManageMenu.NodeName, true, e.ManageMenu.NodeName + e.ManageMenu.Id);
            }
        }


        private void manageControl_MenuQuery(object sender,string text)
        {
            Dictionary<string,List<ManageMenu>> dic = new Dictionary<string,List<ManageMenu>>();

            var list1 = this.manageMenus.FindAll(it => it.NodeType == MenuNodeType.Node && !string.IsNullOrEmpty(it.NodeName) && it.NodeName.IndexOf(text,StringComparison.OrdinalIgnoreCase) > -1).ToList();
            dic.Add("关联到的文档名称",list1);

            var list2 = this.manageMenus.FindAll(it => it.NodeType == MenuNodeType.Node && !string.IsNullOrEmpty(it.Remark) && MarkdownHelper.MarkdownToPlainText(it.Remark).IndexOf(text,StringComparison.OrdinalIgnoreCase) > -1).ToList();
            dic.Add("关联到的内容",list2);

            this.manageControl.SetMenuQueryData(dic);
        }


        private void manageControl_MenuDrag(object sender,MenuEventArgs e)
        {
            e.Cancel = true;
        }

    }
}
